package com.huike.clues.service.impl;


import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.clues.domain.vo.FalseClueVo;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.mapper.TbClueTrackRecordMapper;
import com.huike.clues.service.ITbClueTrackRecordService;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 线索跟进记录Service业务层处理
 *
 * @date 2022-04-22
 */
@Service
public class TbClueTrackRecordServiceImpl implements ITbClueTrackRecordService {

    @Autowired
    private TbClueTrackRecordMapper tbClueTrackRecordMapper;

    @Autowired
    private TbClueMapper tbClueMapper;


    /**
     * 新增线索跟进记录
     *
     * @param clueTrackRecordVo 要新增的数据
     */
    @Override
    public void insertClueTrackRecord(ClueTrackRecordVo clueTrackRecordVo) {
        //获取当前账号姓名
        String username = SecurityUtils.getUsername();
        //获取当前时间
        String nowTime = DateUtils.getTime();
        //添加跟进数据
        tbClueTrackRecordMapper.insertClueTrackRecord(clueTrackRecordVo, username, nowTime);
        //添加客户基本信息
        TbClue tbClue = new TbClue();
        BeanUtils.copyProperties(clueTrackRecordVo, tbClue);
        tbClue.setId(clueTrackRecordVo.getClueId());
        tbClueMapper.updateTbClue(tbClue);
    }

    /**
     * 查询所有跟进记录
     *
     * @param clueId 线索id
     * @return 结果
     */
    @Override
    public List<TbClueTrackRecord> findAll(Long clueId) {
        return tbClueTrackRecordMapper.findAll(clueId);
    }

    /**
     * 伪线索处理
     *
     * @param id          线索id
     * @param falseClueVo 伪线索的原因
     */
    @Override
    public void processingFalseClue(Long id, FalseClueVo falseClueVo) {
        //根据id查询对应线索
        TbClue tbClue = tbClueMapper.selectTbClueById(id);
        //获取线索失败次数
        int falseCount = tbClue.getFalseCount();
        //判断是否大于等于三次
        if (falseCount >= 3) {
            //如果大于等于3次直接删除该线索
            tbClueMapper.deleteTbClueById(id);
            //删除对应线索的跟进记录
            tbClueTrackRecordMapper.deleteRecord(id);
            //删除完直接结束方法
            return;
        }
        //走到这说明没等于三次
        //将失败次数+1
        tbClue.setFalseCount(falseCount += 1);
        //更改线索状态
        tbClue.setStatus(TbClue.StatusType.FALSE.getValue());
        //更新线索
        tbClueMapper.updateTbClue(tbClue);
        //添加新的跟进记录
        //创建对象
        ClueTrackRecordVo clueTrackRecordVo = new ClueTrackRecordVo();
        //设置线索ID
        clueTrackRecordVo.setClueId(id);
        //设置失败原因
        clueTrackRecordVo.setFalseReason(falseClueVo.getReason());
        //设置跟进类型     ("0"是正常跟进  "1"是伪线索)
        clueTrackRecordVo.setType("1");
        //获取当前用户名和时间
        String username = SecurityUtils.getUsername();
        String nowTime = DateUtils.getTime();
        //添加数据
        tbClueTrackRecordMapper.insertClueTrackRecord(clueTrackRecordVo, username, nowTime);
    }


}